package com.xframe.xdal.core.model;


import com.xframe.xdal.core.constant.DalConstants;
import com.xframe.xdal.core.constant.SqlBuilderConstants;
import com.xframe.xdal.core.util.StringUtil;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.util.UUID;

/**
 * 功能描述:表示一个查询条件 where 子句
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-26
 */
@Data
@Log4j2
public class ConditionValue implements IFieldToName{

    /**
     * 名称
     */
    private String keyName;

    /**
     * 比较对象
     */
    private Operator operators;

    /**
     * 值
     */
    private Object val;

    /**
     * 是否使用 %
     */
    private boolean isAutoAddPercentSign = false;

    /**
     * 组名称(如果组名称相同,两个对象将用OR连接 否则AND连接)
     */
    private String groupName;

    /**
     * 构造ES时的附加参数（可以为空）
     */
    private String esQueryBuildArgs;

    /**
     * 获取一个值改值表示组名称，如果为空 则使用UUID填充
     *
     * @return 组名称
     */
    public String getGroupName(){
        if(StringUtil.isEmpty(this.groupName)){
            this.groupName = UUID.randomUUID().toString();
        }
        return this.groupName;
    }

    @Override
    public String getFieldName() {
        return keyName;
    }

    /**
     * 是否一个属性路径
     */
    public boolean isAttrPath(){
        return getKeyName().indexOf(SqlBuilderConstants.SP) > 0;
    }

    public String getOperatorsVal() {
        switch (this.operators) {
            case EQ:
                return " = ";
            case LIKE:
                return " like ";
            case GE:
                return " >= ";
            case GT:
                return " > ";
            case LT:
                return " < ";
            case LE:
                return " <= ";
            case NE:
                return " <> ";
            case IN:
                return " in(";
            case NIN:
                return " not in(";
            case NLIKE:
                return " not like ";
        }
        return " = ";
    }

    public ConditionValue() {
    }

    public ConditionValue(String keyName, Object val) {
        this.keyName = keyName;
        this.operators = Operator.EQ;
        this.val = val;
    }

    public ConditionValue(String keyName, Operator operators, Object val) {
        this.keyName = keyName;
        this.operators = operators;
        this.val = val;
    }

    public ConditionValue(String keyName, Operator operators, Object val, boolean isAutoAddPercentSign) {
        this.keyName = keyName;
        this.operators = operators;
        this.val = val;
        this.isAutoAddPercentSign = isAutoAddPercentSign;
    }

    public ConditionValue(String keyName, Operator operators, Object val, String groupName) {
        this.keyName = keyName;
        this.operators = operators;
        this.val = val;
        this.groupName = groupName;
    }

    public ConditionValue(String keyName, Operator operators, Object val, String groupName, boolean isAutoAddPercentSign) {
        this.keyName = keyName;
        this.operators = operators;
        this.val = val;
        this.isAutoAddPercentSign = isAutoAddPercentSign;
        this.groupName = groupName;
    }

    /**
     * 格式化比较对象值
     * @return value
     */
    public Object getVal() {
        if (isAutoAddPercentSign && (this.operators == Operator.LIKE || this.operators == Operator.NLIKE)) {
            return DalConstants.SQL_PERCENT + val.toString() + DalConstants.SQL_PERCENT;
        }
        return val;
    }

    public Object getValByEs() {
        return val;
    }

    public Object[] getValsByEs() {
        if(this.val == null){
            return new Object[0];
        }
        return val.toString().split(",");
    }
}
